#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <Windows.h>
#include <stdlib.h>
#include <time.h>
#define NUM 101
const char* lable = "|/-\\";

void process()
{
    char buffer[NUM];
    memset(buffer, '\0', sizeof(buffer));
    int len = strlen(lable);
    int count = 0;
    buffer[0] = '>';
    while (count <= 100)
    {
        printf("[%-100s][%3d%%][%c]\r", buffer, count, lable[count % len]);
        fflush(stdout);
        buffer[count++] = '=';
        if (count < 100)
        {
            buffer[count] = '>';
        }
        //Sleep(200);
    }
    printf("\n");
   }

char buffer[101] = { 0 };
void process_flush(double rate)
{
    static int count = 0;
    int n = (int)rate;
    if (n == 0)
        buffer[0] = '>';
    int len = strlen(lable);

    printf("[%-100s][%.1f%%][%c]\r", buffer, rate, lable[count]);
    fflush(stdout);
    count++;
    count %= len;

    buffer[n++] = '=';
    if (n < 100)
    {
        buffer[n] = '>';
    }

    if (rate >= 100.0)
        printf("\n");
}

#define FILESIZE (1024*1024*1024)
void download()
{
    srand(time(NULL) ^ 1023);
    int total = FILESIZE;

    while (total)
    {
        //Sleep(1);
        int one = rand() % (1024 * 1024 * 500);
        total -= one;
        if (total < 0) total = 0;

        int download = FILESIZE - total;
        double rate = (download * 1.0) / FILESIZE * 100.0;

        //printf("download:%f%%\n",rate);

        process_flush(rate);
    }
}

int main()
{
    //process();
    download();
    printf("rand_max:%d", RAND_MAX);
    return 0;
}